package edcom.server;

import edcom.client.Student;

public class AddStudentController {
	
	public Student currentStudent;
	private StudentManager mgr;
	public int counter = 0, dash = 0, flag = 0;
	
	public AddStudentController(){
		this.currentStudent = new Student();
		this.mgr = new StudentManager();
	}
	
	public void setName(String source) throws MalformedStringException
	{
		if((source == null) || (source == "")) 
			throw new MalformedStringException("Input is empty.");
		
		else if(source!=null){
			
			for(int i = 0; i < source.length();i++)
			{
				if(source.charAt(i)=='0' || source.charAt(i)=='1' || 
						source.charAt(i)=='2' || source.charAt(i)=='3' ||
						source.charAt(i)=='4' || source.charAt(i)=='5' || 
						source.charAt(i)=='6' || source.charAt(i)=='7' ||
						source.charAt(i)=='8' || source.charAt(i)=='9' )
						
				{
					throw new MalformedStringException("Numeric input is not allowed.");
				}	
			}
			
		}
		
		// update object field
		currentStudent.setName(source);
		
	}

	public void setSnum(String source)throws MalformedStringException
	{
		if((source == null) || (source == "")) 
			throw new MalformedStringException("Input is empty.");
		else if(source.length()==10){
										
				for(int i = 0; i < source.length();i++)
				{
					if(!(source.charAt(i)=='0') && !(source.charAt(i)=='1') && 
							!(source.charAt(i)=='2') && !(source.charAt(i)=='3') &&
							!(source.charAt(i)=='4') && !(source.charAt(i)=='5') &&
							!(source.charAt(i)=='6') && !(source.charAt(i)=='7') &&
							!(source.charAt(i)=='8') && !(source.charAt(i)=='9') &&
							!(source.charAt(i)=='-') 
					)
					{
						throw new MalformedStringException("Numeric Characters Only.");
					}
					
					if(source.charAt(i)=='-'){
						dash = i;
						counter++;
					}
				}
				
				if(counter!=1 && dash!=4){
					throw new MalformedStringException("Dash.");
				}
				else{
					currentStudent.setStudentNumber(source);
				}
		}
		else{
			throw new MalformedStringException("Wrong student number format.");
		}
		
	}
	
	public void setUsername(String source) throws MalformedStringException
	{
		/*  if source is well-formed, 
		 * 		update object field and return gracefully
		 *  else,
		 *  	throw new MalformedStringException("error description");
		 */
		
		if((source == null) || (source == "")) 
			throw new MalformedStringException("Input is empty.");
		/*else if(source.equals("koffeeaddikt"))
		{
			throw new MalformedStringException("USERNAME already exists.");
		} // */
		else
			currentStudent.setUserName(source);
		
		// insert implementation here
		
	}
	
	public void setPassword(String source) throws MalformedStringException
	{
		/*  if source is well-formed, 
		 * 		update object field and return gracefully
		 *  else,
		 *  	throw new MalformedStringException("error description");
		 */
		
		if((source == null) || (source == "")) 
			throw new MalformedStringException("Input is empty.");
		
		else
			currentStudent.setPassword(source);
		
		// insert implementation here
		
	}
	
	public void setEmail(String source) throws MalformedStringException
	{
		/*  if source is well-formed, 
		 * 		update object field and return gracefully
		 *  else,
		 *  	throw new MalformedStringException("error description");
		 */
		
		if((source == null) || (source == "")) 
			throw new MalformedStringException("Input is empty.");
		/*else if(source.equals("leemasamune@gmail.com"))
		{
			throw new MalformedStringException("EMAIL already exists.");
		} // */
		else if (source!=null)
		{
			for(int i = 0; i < source.length();i++)
			{
				if(source.charAt(i)==' ')
				{
					throw new MalformedStringException("Delete all spaces.");
				}
				
				if(source.charAt(i)=='@'){
					counter++;
					if(source.charAt(i+1)=='.'){
						throw new MalformedStringException("Wrong e-mail format.");
					}
					if(source.charAt(i)=='.'){
						if(source.charAt(i+1)== source.length()){
							throw new MalformedStringException("Wrong e-mail format.");
						}
					}
				}
			}
			if (counter==1){
				currentStudent.setEmail(source);
			}
			else if ( counter > 1){
				throw new MalformedStringException("Wrong e-mail format.");
			}
			else{
				throw new MalformedStringException("Wrong e-mail format.");
			}
		}
	}

	public void setMajor(String source) throws MalformedStringException
	{
		/*  if source is well-formed, 
		 * 		update object field and return gracefully
		 *  else,
		 *  	throw new MalformedStringException("error description");
		 */
		
		if((source == null) || (source == "")) 
			throw new MalformedStringException("Input is empty.");
		
		else if (source!=null){
			
			for(int i = 0; i < source.length();i++)
			{
				if(source.charAt(i)=='0' || source.charAt(i)=='1' || 
						source.charAt(i)=='2' || source.charAt(i)=='3' ||
						source.charAt(i)=='4' || source.charAt(i)=='5' || 
						source.charAt(i)=='6' || source.charAt(i)=='7' ||
						source.charAt(i)=='8' || source.charAt(i)=='9' )
						
				{
					throw new MalformedStringException("Numeric input is not allowed.");
				}
			}
		}
		currentStudent.setMajor(source);
	}
	
	public void addStudent() throws Exception
	{
		if((currentStudent.getName() == "") ||
				(currentStudent.getStudentNumber() == "") ||
				(currentStudent.getEmail() == "") ||
				(currentStudent.getMajor() =="") ||
				(currentStudent.getUserName() == "") ||
				(currentStudent.getPassword() == ""))
		{
			// if it comes to this, bad program!
			throw new IncompleteDataException("Missing data field(s).");
		}
		// automatically throws SQLInsertException
		mgr.insertStudent(currentStudent);  
		//throw new IncompleteDataException("DATA GOOD!");
	}
	
}